package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import java.util.Map;

@Controller
@RequestMapping("/dev")
public class HelloController{
    private static final Logger LOGGER= LoggerFactory.getLogger(HelloController.class);

//    @Override
//    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        LOGGER.info("Spring MVC框架搭建成功，当前请求路径：{}",request.getRequestURI());
//        return new ModelAndView("hello");
//    }
    @RequestMapping("/hello")
    protected ModelAndView hello(@RequestParam(value = "realName",required = false) String name) throws Exception{
        LOGGER.info("你好{}，欢迎学习SpringMVC!",name);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("realName",name);
        modelAndView.setViewName("hello");
        return modelAndView;
    }
    @RequestMapping("/welcome")
    protected String welcome(HttpServletRequest request) throws Exception{
        LOGGER.info("进入控制器welcome方法，请求路径为：{}",request.getRequestURI());
        return "hello";
    }
    @RequestMapping("/model")
    public String mode(Model model,@RequestParam(name = "realName",required = false)String name) throws Exception{
        LOGGER.info("你好{}，在参数注入Model对象也可以出参！",name);
        model.addAttribute("realName",name);
        SysUser sysUser=new SysUser();
        sysUser.setRealName(name);
        model.addAttribute("sysUser",sysUser);
        return "hello";
    }
    @RequestMapping("/map")
    public String model(Map map,@RequestParam(name = "realName",required = false)String name) throws Exception{
        LOGGER.info("你好{}，在参数注入Map对象也可以出参！",name);
        map.put("realName",name);
        SysUser sysUser=new SysUser();
        sysUser.setRealName(name);
        map.put("sysUser",sysUser);
        return "hello";
    }
}
